﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Net.Mail;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        parent par = new parent();
        user_types ut = new user_types();
        protected void Page_Load(object sender, EventArgs e)
        {
            //tbstudid.Text=Request["id"].ToString();
            if (!IsPostBack)
            {
                DataTable dt1 = ut.GetRecords(-99, "", false, " and ");
                DropDownList1.DataSource = dt1;
                DropDownList1.DataTextField = "user_type";
                DropDownList1.DataValueField = "user_type_id";
                DropDownList1.DataBind();

                DataTable dt = par.GetRecords(-99, "", "", "", "", "", "", "", -99, "", -99, "", "", Convert.ToInt64(Request["id"].ToString()),"","", false, " and ");
                if (dt.Rows.Count > 0)
                {
                    Panel1.Visible = false;
                    btninsert.Visible = false;
                }
                else
                {
                    Panel1.Visible = true;
                    btnupdate.Visible = false;
                }
                DetailsView1.DataSource = dt;
                DetailsView1.DataBind();

            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            par.parent_id = -99;
            par.father_name = tbfname.Text;
            par.mother_name = tbmname.Text;
            par.father_qualification = tbfquali.Text;
            par.mother_qualification = tbmquali.Text;
            par.father_occupation = tbfoccu.Text;
            par.mother_occupation = tbmoccu.Text;
            par.permanent_address = tbaddress.Text;
            par.phone_no = Convert.ToInt64(tbpno.Text);
            par.parent_emailid = tbemail.Text;
            par.user_type_id = Convert.ToInt64(DropDownList1.SelectedValue.ToString());
            par.student_id = Convert.ToInt64(Request["id"].ToString());
            par.username = tbusername.Text;
            par.password = tbpassword.Text;
            par.security_question = tbques.Text;
            par.answer = tbanswer.Text;
            par.isdeleted = false;
            par.SaveRecords();
            //mail to parent
            MailMessage MyMessage = new MailMessage();
            MailAddress SendFrom = new MailAddress("eschool123g@gmail.com");
            MyMessage.To.Add(tbemail.Text);
            MyMessage.From = SendFrom;
            MyMessage.Subject = "Your login information.";
            MyMessage.Body = tbusername.Text + "  " + tbpassword.Text;
            System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("eschool123g@gmail.com", "eschool@123");

            SmtpClient emailClient1 = new SmtpClient("smtp.gmail.com");
            emailClient1.EnableSsl = true;
            emailClient1.Port = 587;
            emailClient1.Credentials = basicAuthenticationInfo;
            emailClient1.Send(MyMessage);

            Response.Redirect("~/admin/Student.aspx");

        }

        

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            par.parent_id = Convert.ToInt64(tbparid.Text);
            par.father_name = tbfname.Text;
            par.mother_name = tbmname.Text;
            par.father_qualification = tbfquali.Text;
            par.mother_qualification = tbmquali.Text;
            par.father_occupation = tbfoccu.Text;
            par.mother_occupation = tbmoccu.Text;
            par.permanent_address = tbaddress.Text;
            par.phone_no = Convert.ToInt64(tbpno.Text);
            par.parent_emailid = tbemail.Text;
            par.user_type_id = Convert.ToInt64(DropDownList1.SelectedValue.ToString());
            par.student_id = Convert.ToInt64(Request["id"].ToString());
            par.username = tbusername.Text;
            par.password = tbpassword.Text;
            par.security_question = tbques.Text;
            par.answer = tbanswer.Text;
            par.isdeleted = false;
            par.SaveRecords();
            Response.Redirect("~/admin/Student.aspx");
        }

        protected void btnedit_Command1(object sender, CommandEventArgs e)
        {
            Panel1.Visible = true;
            DataTable dt = par.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", "", "", "", "", "", -99, "", -99, "", "", -99,"","", false, " and ");
            tbparid.Text = dt.Rows[0]["parent_id"].ToString();
            tbfname.Text = dt.Rows[0]["father_name"].ToString();
            tbmname.Text = dt.Rows[0]["mother_name"].ToString();
            tbfquali.Text = dt.Rows[0]["father_qualification"].ToString();
            tbmquali.Text = dt.Rows[0]["mother_qualification"].ToString();
            tbfoccu.Text = dt.Rows[0]["father_occupation"].ToString();
            tbmoccu.Text = dt.Rows[0]["mother_occupation"].ToString();
            tbaddress.Text = dt.Rows[0]["permanent_address"].ToString();
            tbpno.Text = dt.Rows[0]["phone_no"].ToString();
            tbemail.Text = dt.Rows[0]["parent_emailid"].ToString();
            DropDownList1.SelectedValue = dt.Rows[0]["user_type_id"].ToString();
            tbstudid.Text = dt.Rows[0]["student_id"].ToString();
            tbusername.Text = dt.Rows[0]["username"].ToString();
            tbpassword.Text = dt.Rows[0]["password"].ToString();
            tbques.Text = dt.Rows[0]["security_question"].ToString();
            tbanswer.Text = dt.Rows[0]["answer"].ToString();
            tbusername.Visible = false; tbpassword.Visible = false;
            lbpass.Visible = false; lbuser.Visible = false;
        }

        protected void btndelete_Command1(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = par.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", "", "", "", "", "", -99, "", -99, "", "", -99,"","", false, " and ");
                par.parent_id = Convert.ToInt64(dt.Rows[0]["parent_id"].ToString());
                par.father_name = dt.Rows[0]["father_name"].ToString();
                par.mother_name = dt.Rows[0]["mother_name"].ToString();
                par.father_qualification = dt.Rows[0]["father_qualification"].ToString();
                par.mother_qualification = dt.Rows[0]["mother_qualification"].ToString();
                par.father_occupation = dt.Rows[0]["father_occupation"].ToString();
                par.mother_occupation = dt.Rows[0]["mother_occupation"].ToString();
                par.permanent_address = dt.Rows[0]["permanent_address"].ToString();
                par.phone_no = Convert.ToInt64(dt.Rows[0]["phone_no"].ToString());
                par.parent_emailid = dt.Rows[0]["parent_emailid"].ToString();
                par.user_type_id = Convert.ToInt64(dt.Rows[0]["user_type_id"].ToString());
                par.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                par.username = dt.Rows[0]["username"].ToString();
                par.password = dt.Rows[0]["password"].ToString();
                par.security_question = dt.Rows[0]["security_question"].ToString();
                par.answer = dt.Rows[0]["answer"].ToString();
                par.isdeleted = true;
                par.SaveRecords();
                Response.Redirect("~/admin/Student.aspx");
            }
        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {

        }

        protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
        {

        }
    }
}
